import 'package:flutter/material.dart';
import 'package:get/get.dart';

class BGQuizIndexPage extends StatefulWidget {
  BGQuizIndexPage({Key? key, required this.indexList, required this.undoneNumList, this.getIndex}) : super(key: key);
  final Function(int, int)? getIndex;
  final List<List<String>> indexList;
  final List undoneNumList;

  @override
  State<BGQuizIndexPage> createState() => _BGExamWidgetState();
}

class _BGExamWidgetState extends State<BGQuizIndexPage> {
  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var indexContentBorder = const BoxDecoration(
      border: Border.fromBorderSide(BorderSide(width: 1, color: Colors.black12, style: BorderStyle.solid)),
    );
    var indexContentShape = const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(5)));
    var actions = <Widget>[
      IconButton(
        icon: const Icon(Icons.cancel),
        color: Get.isDarkMode ? Colors.white : Colors.red,
        iconSize: 40,
        onPressed: () {
          Get.back();
        },
      ),
    ];
    return AlertDialog(
      title: Text('QUESTION INDEX'.tr),
      titleTextStyle: TextStyle(fontWeight: FontWeight.bold, color: Get.isDarkMode ? Colors.white : Colors.black, fontSize: 24),
      backgroundColor: Get.isDarkMode ? Colors.black : Colors.white,
      shape: indexContentShape,
      content: Container(
          decoration: indexContentBorder,
          padding: const EdgeInsets.all(8.0),
          width: 420,
          height: 420,
          child: Scrollbar(thumbVisibility: true, child: SingleChildScrollView(primary: true, child: _getSectionListContainer()))),
      actions: actions,
      // contentPadding: const EdgeInsets.all(0.0),
      buttonPadding: const EdgeInsets.all(0.0),
      actionsPadding: const EdgeInsets.all(0.0),
    );
  }

  Container _getSectionListContainer() {
    var sectionList = widget.indexList.asMap().entries.map((entries) {
      var section = Column(
          crossAxisAlignment: CrossAxisAlignment.start, children: [SizedBox(height: 10), _getQuestionListContainer(entries.value, entries.key)]);
      return section;
    }).toList();

    var sectionListContainer = Container(
        padding: const EdgeInsets.all(10.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [...sectionList],
        ));
    return sectionListContainer;
  }

  Wrap _getQuestionListContainer(List eSection, int sectionNum) {
    List<Widget> questionList = eSection
        .asMap()
        .entries
        .map((e) {
          bool isUndo = false;
          for (String numStr in widget.undoneNumList) {
            if (numStr.contains('.')) {
              if (e.value == (numStr + ' ')) {
                isUndo = true;
                break;
              }
            } else {
              if (e.value == (numStr + '. ')) {
                isUndo = true;
                break;
              }
            }
          }
          return Container(
              padding: const EdgeInsets.all(5),
              child: GestureDetector(
                  onTap: () {
                    Get.back();
                    onQuestionItemClick(sectionNum, e.key);
                  },
                  child: Text(e.value,
                      style: TextStyle(
                          fontWeight: FontWeight.normal,
                          color: isUndo ? (Get.isDarkMode ? Colors.white70 : Colors.red) : (Get.isDarkMode ? Colors.white38 : Colors.black87),
                          fontSize: 18))));
        })
        .cast<Widget>()
        .toList();
    var questionListContainer = Wrap(spacing: 2, runSpacing: 2, children: questionList);
    return questionListContainer;
  }

  void onQuestionItemClick(int sectionNum, int questionNum) {
    widget.getIndex!(sectionNum, questionNum);
  }
}
